<?php

namespace App\Http\Controllers\Api;

use App\GroupBuy;
use App\GroupMaster;
use App\Http\Controllers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Sku;
use Carbon\Carbon;
use Illuminate\Http\Request;

class GroupBuyController extends Controller
{
    use ApiResponse;
    public function index(Request $request)
    {
        $result=GroupBuy::with('goods')->where('status',1)->paginate($request->get('size',10));
        $result->transform(function ($item){
            $item['price']=ceil($item->goods->price*$item->price_discount/100);
            return $item;

        });
        return $this->data($result);
    }
    public function show($id)
    {
        $result=GroupBuy::with(['goods'=>function($query){
            $query->with('skus');
        }])->where('id',$id)->where('status',1)->first();
        $result->price=ceil($result->goods->price*$result->price_discount/100);
        $where=[
            ['group_buy_id','=',$id],
            ['status','=',1],
            ['over','=',0]
        ];
        $list=GroupMaster::with('user')->where('end_time','>',Carbon::now())->where('over',0)->latest()->where($where)->get();
        $result->list=$list;
        return $this->data($result);
    }
}
